package com.xbongbong.paas.util;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.pro.constant.BusinessConstant;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.ContractEnum;
import org.elasticsearch.index.query.BoolQueryBuilder;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
import static org.elasticsearch.index.query.QueryBuilders.existsQuery;
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
import static org.elasticsearch.index.query.QueryBuilders.termsQuery;

/**
 * @author 吴峰
 * @date 2022/03/21 15:08
 */
public class FieldBoolQueryUtil {

    public static Map<String, Object> getFieldData(Integer fieldType, String attrStr, String corpid, Long formId, Integer businessType) {
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);

        fieldValueIsNull(fieldType, param);
        param.put("attr", attrStr);
        param.put(StringConstant.CORPID, corpid);
        param.put("formId", formId);
        param.put("businessType", businessType);
        param.put("pageNum", 100);
        param.put("start", 0);
        return param;
    }


    /**
     * 字段是否存在参数拼装
     * @param saasMark
     * @param businessType
     * @param corpid
     * @param formId
     * @param fieldType
     * @param isSub 1:子表单；2：关联产品；0或者null：主数据
     * @param attrs
     * @return
     */
    public static BoolQueryBuilder fieldExistDataBoolQuery(Integer saasMark, Integer businessType, String corpid, Long formId, Integer fieldType, Integer isSub, String[] attrs) {
        // 处理列表数据, 普通子表单数据
        BoolQueryBuilder boolQueryBuilder = boolQuery();

        boolQueryBuilder.filter(termQuery("corpid.keyword", corpid));
        if (Objects.equals(BasicConstant.ONE, isSub) && XbbRefTypeEnum.hasRecyleBin(saasMark, businessType)) {
            // 有回收站的子表单
            boolQueryBuilder.filter(termsQuery("del", Arrays.asList(DelEnum.NORMAL.getDel(), DelEnum.DELETE_SUBFORM.getDel())));
        } else if (XbbRefTypeEnum.hasRecyleBin(saasMark, businessType)) {
            // 有回收站的主表数据
            boolQueryBuilder.filter(termsQuery("del", Arrays.asList(DelEnum.NORMAL.getDel(), DelEnum.DELETE.getDel())));
        } else {
            boolQueryBuilder.filter(termQuery("del", DelEnum.NORMAL.getDel()));
        }
        boolQueryBuilder.filter(termQuery("formId", formId));

        String esAttrStr;
        if (Objects.equals( BasicConstant.ONE, isSub)) {
            // 子表单
            boolQueryBuilder.filter(termQuery(StringConstant.ATTR + StringConstant.POINT + StringConstant.KEY_WORD, attrs[0]));
            esAttrStr = StringConstant.JSON_DATA + StringConstant.POINT + attrs[1];
        } else if (Objects.equals( BasicConstant.TWO, isSub)) {
            // 关联产品
            esAttrStr = StringConstant.JSON_DATA + StringConstant.POINT + attrs[1];
        } else {
            esAttrStr = StringConstant.JSON_DATA + StringConstant.POINT + attrs[0];
        }
        boolQueryBuilder.filter(existsQuery(esAttrStr));

        Boolean isAddress = Objects.equals(FieldTypeEnum.ADDRESS.getType(), fieldType) || Objects.equals(FieldTypeEnum.LOCATION.getType(), fieldType);
        Boolean isFile = Objects.equals(FieldTypeEnum.IMAGE.getType(), fieldType) || Objects.equals(FieldTypeEnum.UPLOAD.getType(), fieldType);
        if (!Objects.equals(FieldTypeEnum.NUM.getType(), fieldType) && !ExplainUtil.isSubForm(fieldType) && !isAddress && !isFile) {
            esAttrStr += StringConstant.POINT + StringConstant.KEY_WORD;
        }
        // 不允许为空
        FieldBoolQueryUtil.fieldValueIsNull(fieldType, esAttrStr, boolQueryBuilder);
        boolean notSub = Objects.equals(BasicConstant.ZERO, isSub) || Objects.isNull(isSub);
        if (notSub && Objects.equals(SaasMarkEnum.SAAS.getCode(), saasMark) && Objects.equals(XbbRefTypeEnum.CONTRACT.getCode(), businessType)) {
            // 合同中检索的是非红冲的合同
            boolQueryBuilder.filter(termQuery(StringConstant.JSON_DATA + StringConstant.POINT + ContractEnum.IS_RED.getAttr(), 0));
        }

        return boolQueryBuilder;
    }


    public static void fieldValueIsNull(Integer fieldType, String esAttrStr, BoolQueryBuilder boolQueryBuilder) {
        // 不允许为空
        if (Objects.equals(FieldTypeEnum.LINK_DATA.getType(), fieldType)) {
            // 关联数据，判断字段存在或者非0
            boolQueryBuilder.filter(boolQuery().mustNot(termQuery(esAttrStr,0)));
            boolQueryBuilder.filter(boolQuery().mustNot(termQuery(esAttrStr,"")));
        } else if (Objects.equals(FieldTypeEnum.ADDRESS.getType(), fieldType)
                || Objects.equals(FieldTypeEnum.LOCATION.getType(), fieldType)) {
            // 地址或者定位
            boolQueryBuilder.filter(boolQuery().mustNot(termQuery(esAttrStr + StringConstant.POINT + BusinessConstant.PROVINCE + StringConstant.POINT + StringConstant.KEY_WORD,"")));
        } else if (Objects.equals(FieldTypeEnum.UPLOAD.getType(), fieldType)) {
            boolQueryBuilder.filter(boolQuery().mustNot(termQuery(esAttrStr,"[]")));
        } else if (Objects.equals(FieldTypeEnum.IMAGE.getType(), fieldType)) {
            boolQueryBuilder.filter(boolQuery().mustNot(termQuery(esAttrStr,"[]")));
        } else if (Objects.equals(FieldTypeEnum.SUB_FORM.getType(), fieldType)) {
            boolQueryBuilder.filter(boolQuery().mustNot(termQuery(esAttrStr, new ArrayList<>())));
        } else if (!Objects.equals(FieldTypeEnum.NUM.getType(), fieldType)) {
            boolQueryBuilder.filter(boolQuery().mustNot(termQuery(esAttrStr,"")));
        }
    }

    public static void fieldValueIsNull(Integer fieldType, Map<String, Object> param) {
        if (Objects.equals(FieldTypeEnum.LINK_DATA.getType(), fieldType)) {
            // 关联数据，判断字段存在或者非0
            param.put("value", 0);
        } else if (Objects.equals(FieldTypeEnum.ADDRESS.getType(), fieldType)
                || Objects.equals(FieldTypeEnum.LOCATION.getType(), fieldType)) {
            // 地址或者定位
            param.put("value", "");
        } else if (Objects.equals(FieldTypeEnum.COMBO_CHECK.getType(), fieldType)
                || Objects.equals(FieldTypeEnum.CHECKBOX_GROUP.getType(), fieldType)
                || Objects.equals(FieldTypeEnum.USER_GROUP.getType(), fieldType)
                || Objects.equals(FieldTypeEnum.DEPT_GROUP.getType(), fieldType)
                || Objects.equals(FieldTypeEnum.IMAGE.getType(), fieldType)
                || ExplainUtil.isSubForm(fieldType)
        ) {
            param.put("array", 1);
        }  else if (Objects.equals(FieldTypeEnum.UPLOAD.getType(), fieldType)) {
            param.put("value", "[]");
        } else if (!Objects.equals(FieldTypeEnum.NUM.getType(), fieldType)) {
            param.put("value", "");
        }
    }
}
